package com.jetinno.utils;

import android.support.annotation.IntRange;
import android.text.TextUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

/**
 * Created by zhangyuncai on 2019/4/11.
 * 解决精度问题
 */
public class NumberUtil {

    private static DecimalFormat threelastDF = new DecimalFormat("#0.000");
    private static DecimalFormat twolastDF = new DecimalFormat("#0.00");
    private static DecimalFormat onelastDF = new DecimalFormat("#0.0");
    private static DecimalFormat IntlastDF = new DecimalFormat("#0");

    static {
//        DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
        DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.ENGLISH);
        decimalFormatSymbols.setDecimalSeparator('.');
        onelastDF.setDecimalFormatSymbols(decimalFormatSymbols);
        twolastDF.setDecimalFormatSymbols(decimalFormatSymbols);
        IntlastDF.setDecimalFormatSymbols(decimalFormatSymbols);
        threelastDF.setDecimalFormatSymbols(decimalFormatSymbols);
    }

    public static DecimalFormat getInstant() {
        DecimalFormat df = new DecimalFormat();
//        DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
        DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.ENGLISH);
        decimalFormatSymbols.setDecimalSeparator('.');
        df.setDecimalFormatSymbols(decimalFormatSymbols);
        return df;
    }

    public static DecimalFormat getInstant(String pattern) {
        DecimalFormat df = new DecimalFormat(pattern);
//        DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
        DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.ENGLISH);
        decimalFormatSymbols.setDecimalSeparator('.');
        df.setDecimalFormatSymbols(decimalFormatSymbols);
        return df;
    }

    public static float parseFloat(String strIn) {
        float l = 0;
        if (TextUtils.isEmpty(strIn)) {
            return l;
        }
        try {
            l = Float.parseFloat(strIn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return l;
    }

    public static int parseInteger(String strIn) {
        int l = 0;
        if (TextUtils.isEmpty(strIn)) {
            return l;
        }
        try {
            l = Integer.parseInt(strIn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return l;
    }

    public static int parseInteger(String strIn, int hex) {
        int l = 0;
        if (TextUtils.isEmpty(strIn)) {
            return l;
        }
        try {
            l = Integer.parseInt(strIn, hex);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return l;
    }

    public static long parseLong(String strIn) {
        long l = 0;
        if (TextUtils.isEmpty(strIn)) {
            return l;
        }
        try {
            l = Long.parseLong(strIn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return l;
    }

    public static double parseDouble(String strIn) {
        double l = 0.0;
        if (TextUtils.isEmpty(strIn)) {
            return l;
        }
        try {
            l = Double.parseDouble(strIn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return l;
    }

    private static String removeChar(String inputStr) {
        return inputStr.replaceAll(",", "");
    }

    /**
     * 金钱加法
     */
    public static String add(String v1, String v2) {
        String add = null;
        try {
            v1 = removeChar(v1);
            v2 = removeChar(v2);
            BigDecimal b1 = new BigDecimal(v1);
            BigDecimal b2 = new BigDecimal(v2);
            add = b1.add(b2).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (add == null) {
            add = "";
        }
        return add;
    }

    /**
     * 金钱减法
     */
    public static String subtract(String v1, String v2) {
        String subtract = null;
        try {
            v1 = removeChar(v1);
            v2 = removeChar(v2);
            BigDecimal b1 = new BigDecimal(v1);
            BigDecimal b2 = new BigDecimal(v2);
            subtract = b1.subtract(b2).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (subtract == null) {
            subtract = "";
        }
        return subtract;
    }

    /**
     * 金钱乘法
     */
    public static String multiply(String v1, String v2) {
        String multiply = null;
        try {
            v1 = removeChar(v1);
            v2 = removeChar(v2);
            BigDecimal b1 = new BigDecimal(v1);
            BigDecimal b2 = new BigDecimal(v2);
            multiply = b1.multiply(b2).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (multiply == null) {
            multiply = "";
        }
        return multiply;
    }

    /**
     * 乘法
     *
     * @param v1    乘数
     * @param v2    被乘数
     * @param scale 小数点保留位数
     */
    public static String multiplyWithScale(String v1, String v2, int scale) {
        String multiplyWithScale = null;
        try {
            v1 = removeChar(v1);
            v2 = removeChar(v2);
            BigDecimal b1 = new BigDecimal(v1);
            BigDecimal b2 = new BigDecimal(v2);
            BigDecimal result = b1.multiply(b2);
            result = result.setScale(scale);
            multiplyWithScale = result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (multiplyWithScale == null) {
            multiplyWithScale = "";
        }
        return multiplyWithScale;
    }

    /**
     * 金钱除法
     */
    public static String divide(String v1, String v2) {
        String divide = null;
        try {
            v1 = removeChar(v1);
            v2 = removeChar(v2);
            BigDecimal b1 = new BigDecimal(v1);
            BigDecimal b2 = new BigDecimal(v2);
            divide = b1.divide(b2).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (divide == null) {
            divide = "";
        }
        return divide;
    }

    public static String threelastDF(double d) {
        threelastDF.setRoundingMode(RoundingMode.HALF_UP);//要四舍五入
        String dText = String.valueOf(d);
        String d2Text = threelastDF.format((new BigDecimal(dText)));
        return d2Text;
    }

    public static double threelastDouble(double d) {
        return parseDouble(threelastDF(d));
    }

    public static String twolastDF(double d) {
        twolastDF.setRoundingMode(RoundingMode.HALF_UP);//要四舍五入
        String dText = String.valueOf(d);
        String d2Text = twolastDF.format((new BigDecimal(dText)));
        return d2Text;
    }

    public static double twolastDouble(double d) {
        return parseDouble(twolastDF(d));
    }

    public static String onelastDF(double d) {
        onelastDF.setRoundingMode(RoundingMode.HALF_UP);//要四舍五入
        String dText = String.valueOf(d);
        String d2Text = onelastDF.format((new BigDecimal(dText)));
        return d2Text;
    }

    public static double onelastDouble(double d) {
        return parseDouble(onelastDF(d));
    }

    public static String IntlastText(double d) {
        IntlastDF.setRoundingMode(RoundingMode.HALF_UP);//要四舍五入
        String dText = String.valueOf(d);
        String d2Text = IntlastDF.format((new BigDecimal(dText)));
        return d2Text;
    }

    public static int Intlast(double d) {
        return parseInteger(IntlastText(d));
    }

    /**
     * 千位分隔符,并且小数点后保留2位
     *
     * @param strIn 传入值
     * @param num   保留多少位
     * @return String
     */
    public static String formatNum(String strIn, @IntRange(from = 1, to = 3) int num) {
        if (TextUtils.isEmpty(strIn)) {
            return "";
        }
        String strOut;
        try {
            Double n = Double.valueOf(strIn);
            DecimalFormat df;
            if (num == 1) {
                df = getInstant("#,##0.0");
            } else if (num == 2) {
                df = getInstant("#,##0.00");
            } else {
                df = getInstant("#,##0.000");
            }
            strOut = df.format(n);
            if (strOut.indexOf(".") > 0) {
                //正则表达
                strOut = strOut.replaceAll("0+?$", "");//去掉后面无用的零
                strOut = strOut.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
            }
        } catch (Exception e) {
            e.printStackTrace();
            return strIn;
        }
        return strOut;
    }
}
